package com.zhen.Controller_1;

import com.zhen.Mapper.AdminMapper;
import com.zhen.Mapper.OtherMapper;
import com.zhen.My.MyOperate;
import com.zhen.Service_2.Operate;
import com.zhen.Service_2.Other;
import com.zhen.Service_2.Student2;
import com.zhen.Service_2.Teacher2;
import com.zhen.User.*;
import com.zhen.Utils.JwtUtils;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class Main1 {
    @Autowired
    private Student2 student;
    @Autowired
    private Teacher2 teacher;
    @Autowired
    private Result result;
    @Autowired
    private Other other;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private Operate operate;
//    登录
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin){
        //初次登录,验证用户名,密码是否正确
        //正确 返回用户所有信息
        Admin admin1=adminMapper.login(admin);
        //生成token,向前端保存值(前端只保存token包含用户名,id)
        Map<String,Object> map=new HashMap<>();
        map.put("name",admin1.getName());
        map.put("id",admin1.getId());
        return result.success(JwtUtils.settoken(map));
        //在后续执行时,发起请求会进行拦截
        //验证是否有token,解析是否正确...,不需要确认传递的name,id是否正确
        //因为解析token有专门的解密规则(zhenzhencao)一旦解析错误,证明token错误
    }
    @PostMapping("/zhuce")
    public Result zhuce(@RequestBody Admin admin){
        return result.success(adminMapper.insert(admin));
    }
    //查询所有学生信息
    @MyOperate
    @GetMapping("/selectstudentAll")
    public Result student() {
        List<Student> list=student.stselectall();
        return result.success(list);
    }
    //分页查询学生信息  //要总和及各个详细学生信息
    //未用
    @MyOperate
    @GetMapping("/selectstudentpage/{begin}/{end}")
    public Result studentpage(@PathVariable int begin, @PathVariable int end) {
        Fengye fy=student.stselectpage(begin,end);
        return result.success(fy);
    }
//查询
    @MyOperate
    @GetMapping("/selectstudentcha/{begin}/{end}")
    public Result selectstudentcha(@PathVariable int begin, @PathVariable int end,@RequestParam(name="id",required = false) Integer id,@RequestParam(name="name",required = false) String name, @RequestParam(name="classroom",required = false) String classroom, @RequestParam(name="sex",required = false) String sex) {
        Fengye fy=student.stselectcha(id, name, classroom, sex,begin,end);
        return result.success(fy);
    }
    @MyOperate
    @GetMapping("/delstudent/{list}")
    public Result delstudent(@PathVariable List<Integer> list) {
        student.deleteStudent(list);
        return result.success();
    }
    //更新学生信息
    @MyOperate
    @PostMapping("/updatestudent")
    public Result updatestudent(@RequestBody Student students) {
        student.updateStudent(students);
        System.out.println(students);
        return result.success();
    }
//    更新学生成绩
@MyOperate
@PostMapping("/updatescore")
public Result updatescore(@RequestBody Score score) {
    student.updateScore(score);
    return result.success();
}
//    添加学生
    @MyOperate
    @PostMapping("/insertstudent")
    public Result insertstudent(@RequestBody Student students) {
        //添加学生成绩,默认为null
        //添加学生
        return result.success(student.insertStudent(students));
    }
//重置学生密码
    @MyOperate
    @GetMapping("/updatepwdstudent/{id}")
    public Result updatepwdstudent(@PathVariable int id) {
        ;
        return result.success(student.updatepwdstudent(id));
    }
    //    学生成绩分页查找
    @MyOperate
    @GetMapping("/scoreselect/{begin}/{end}")
    public Result scoreselect(@PathVariable int begin, @PathVariable int end,@RequestParam(name="id",required = false) Integer id,@RequestParam(name="name",required = false) String name,@RequestParam(name="classroom",required = false) String classroom){
        Fengye fy=student.scoreselect(id, name, classroom,begin,end);
        return  result.success(fy);
    }
    //----------
    //老师
    @MyOperate
    @GetMapping("/selectteacherAll")
    public Result teacher() {
        List<Teacher> list=teacher.teselectall();
        return result.success(list);
    }
    //分页查询老师信息  //要总和及各个详细老师信息
    @MyOperate
    @GetMapping("/selectteacherpage/{begin}/{end}")
    public Result teacherpage(@PathVariable int begin, @PathVariable int end) {
        Fengye fy=teacher.teselectpage(begin,end);
        return result.success(fy);
    }
    //查询
    @MyOperate
    @GetMapping("/selectteachercha/{begin}/{end}")
    public Result selectteachercha(@PathVariable int begin, @PathVariable int end,@RequestParam(name="id",required = false) Integer id,@RequestParam(name="name",required = false) String name, @RequestParam(name="sex",required = false) String sex) {
        System.out.println(id);
        System.out.println(name);
        Fengye fy=teacher.teselectcha(id, name, sex,begin,end);
        return result.success(fy);
    }
    @MyOperate
    @GetMapping("/delteacher/{list}")
    public Result delteacher(@PathVariable List<Integer> list) {
        teacher.deleteTeacher(list);
        return result.success();
    }
    @MyOperate
    @PostMapping("/updateteacher")
    public Result updateteacher(@RequestBody Teacher teachers) {

        teacher.updateTeacher(teachers);
        return result.success();
    }


    //    添加老师
    @MyOperate
    @PostMapping("/insertteacher")
    public Result insertteacher(@RequestBody Teacher teachers) {

        return result.success(teacher.insertTeacher(teachers));
    }
    //重置老师密码
    @MyOperate
    @GetMapping("/updatepwdteacher/{id}")
    public Result updatepwdteacher(@PathVariable int id) {

        return result.success(teacher.updatepwdteacher(id));
    }
    //---------------------日历
    @MyOperate
    @GetMapping("/date")
    public Result date() {
        Map<String,Object> map=new HashMap<>();
        for (Riqi riqi : other.riqi()) {
            map.put(riqi.getRiqi(),riqi.getContent());
        }
        return result.success(map);
    }
    //添加日期信息
    @MyOperate
    @PostMapping("/insertriqi")
    public Result insertriqi(@RequestBody Riqi riqi) {
        System.out.println(riqi);
        other.insertriqi(riqi);
        return result.success();
    }
//    删除日历信息
@MyOperate
@GetMapping("/delrili/{tian}")
public Result delriqi(@PathVariable String tian) {
    other.delriqi(tian);
    return result.success();
}
    //查找课表 班级1
    @MyOperate
    @GetMapping("/class1")
    public Result class1() {
        Map<String,Object> map=new HashMap<>();
        for (Riqi riqi : other.class1()) {
            map.put(riqi.getRiqi(),riqi.getContent());
        }
        return result.success(map);
    }
    //查找课表 班级2
    @MyOperate
    @GetMapping("/class2")
    public Result class2() {
        Map<String, Object> map = new HashMap<>();
        for (Riqi riqi : other.class2()) {
            map.put(riqi.getRiqi(), riqi.getContent());
        }
        return result.success(map);
    }
    //添加课表
    @MyOperate
    @PostMapping("/insertclass1")
    public Result insertclass1(@RequestBody Riqi riqi) {
        System.out.println(riqi);
        other.insertclass1(riqi);
        return result.success();
    }
    @MyOperate
    @PostMapping("/insertclass2")
    public Result insertclass2(@RequestBody Riqi riqi) {
        System.out.println(riqi);
        other.insertclass2(riqi);
        return result.success();
    }
    //    删除class1信息
    @MyOperate
    @GetMapping("/delclass1/{tian}")
    public Result delclass1(@PathVariable String tian) {
        other.delclass1(tian);
        return result.success();
    }
    //    删除class2信息
    @MyOperate
    @GetMapping("/delclass2/{tian}")
    public Result delclass2(@PathVariable String tian) {
        other.delclass2(tian);
        return result.success();
    }
    @GetMapping("/rizhi/{begin}/{end}")
    public Result rizhi(@PathVariable Integer begin,@PathVariable Integer end) {
        Fengye fy=operate.selectAll(begin,end);
        return result.success(fy);
    }
    @GetMapping("/studentcount")
    public Result studentcount() {
       return result.success(student.studentcount());
    }
    @GetMapping("/teachercount")
    public Result teachercount() {
        return result.success(teacher.teachercount());
    }
    }
